package com.heisenhub.common.db.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heisenhub.common.db.enums.IsDelEnum;
import com.heisenhub.common.db.mapper.SuperBaseMapper;
import com.heisenhub.common.db.model.IsDel;
import com.heisenhub.common.db.service.ISuperService;
import com.heisnenhub.common.module.entity.PageQuery;
import com.heisnenhub.common.module.utils.ThrowExceptionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author sen
 * @description
 * @date 2024/1/11 17:43
 */
@Service
public class SuperServiceImpl <M extends SuperBaseMapper<T,Q>, T extends IsDel, Q extends PageQuery> extends ServiceImpl<M, T> implements ISuperService<T,Q> {

    /**
     *  软删除
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean delById(Serializable id) {
        T byId = findById(id);
        ThrowExceptionUtils.isTure(Objects.isNull(byId)).throwMessage("对象不存在 or 对象已删除");
        byId.setDel(IsDelEnum.DELETE);
        return  this.updateById(byId);
    }

    /**
     *   查找不是删除的数据
     * @param id
     * @return
     */
    @Override
    public T findById(Serializable id) {
        T byId = this.getById(id);
        if(Objects.nonNull(byId) && byId.getDel().equals(IsDelEnum.NORMAL)) return byId;
        return null;
    }

    @Override
    public T findByIdThrow(Serializable id) {
        T byId = findById(id);
        ThrowExceptionUtils.isTure(Objects.isNull(byId)).throwMessage("对象不存在 or 对象已删除");
        return byId;
    }

    /**
     *  分页查询
     * @param pageQuery   查询对象
     * @return
     */
    @Override
    public IPage<T> doPage(Q pageQuery) {
        return  this.baseMapper.doPage(new Page(pageQuery.getPageNo(), pageQuery.getPageSize()), pageQuery);
    }



}
